package com.example.securityconfig.config;


import com.alibaba.fastjson.JSON;
import com.example.securityconfig.entity.Result;
import com.example.securityconfig.entity.enums.ExceptionCodeEnum;
import com.example.securityconfig.util.WebUtils;


import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
* 权限校验处理器
* */
@Component
public class SecurityAccessDeniedHandlerExceptionHandler implements AccessDeniedHandler {
 
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        //  NO_AUTHORITY(500206, "权限校验失败");
        Result<Object> error = Result.error(ExceptionCodeEnum.ROOT_ERROR);
        String s = JSON.toJSONString(error);
        // 处理异常
        WebUtils.renderString(response, s);
    }
}